package com.example.ai_agent.rag;

import jakarta.annotation.Resource;

import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

import static org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgDistanceType.COSINE_DISTANCE;
import static org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIndexType.HNSW;

/**
 * @author Yan.z.h
 * @version 1.0
 */
@Configuration
public class PgVectorVectorStoreConfig {
    @Resource
    LoveAppDocumentLoader loveAppDocumentLoader;

    @Bean
    public VectorStore pgVectorVectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel dashscopeEmbeddingmodel) {
        PgVectorStore vectoreStore = PgVectorStore.builder(jdbcTemplate, dashscopeEmbeddingmodel)
                .dimensions(1536)
                .distanceType(COSINE_DISTANCE)
                .indexType(HNSW)
                .initializeSchema(true)
                .vectorTableName("vectore_store")
                .maxDocumentBatchSize(10000)
                .build();
        List<Document> aLlDocement = loveAppDocumentLoader.getALlDocement();
        vectoreStore.add(aLlDocement);
        return vectoreStore;

    }
}
